package controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import po.User;
import redis.clients.jedis.Jedis;
import utils.FlushUuid;
import service.UserService;
import utils.ServerResponse;
import utils.UuidUtils;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/26 14:17
 * @description:
 */

@Controller
@RequestMapping("/manage/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FlushUuid flushUuid;

    //登录
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    @ResponseBody
    public ServerResponse login(
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "password", required = false) String password,
            HttpSession session) {
        // userService
        // if success -> ServerResponse<User>
        // if error   -> ServerResponse.message = 密码错误
        ServerResponse<User> response = userService.login(username, password);
        if (response.success()) {
            // save to session
            session.setAttribute("user", response.getData());
        }

        return response;
    }

    //注册
    @RequestMapping(method = RequestMethod.POST, value = "/register")
    @ResponseBody
    public ServerResponse register(User user) throws Exception {
        ServerResponse userResponse = userService.insert(user);
        return userResponse;
    }

    //检查用户名是否有效
    @RequestMapping(method = RequestMethod.POST, value = "/check_valid.do")
    @ResponseBody
    public ServerResponse check_valid(String str, String type) {
        ServerResponse serverResponse = userService.check_valid(str, type);
        return serverResponse;
    }

    //获取登录用户信息
    @RequestMapping(method = RequestMethod.POST, value = "/get_user_info")
    @ResponseBody
    public ServerResponse register(HttpSession session, @RequestParam(value = "username", required = false) String username, @RequestParam(value = "password", required = false) String password) {

        ServerResponse<User> userResponse = userService.check(username, password);
        if (session == null) {
            userResponse.setStatus(1);
            userResponse.setMsg("用户未登录,无法获取当前用户信息");
        }
        if (userResponse.success()) {
            session.setAttribute("date", userResponse.getData());
        }
        return userResponse;
    }

    //忘记密码
    @RequestMapping(method = RequestMethod.POST, value = "/forget_get_question")
    @ResponseBody
    public ServerResponse forGet(@RequestParam(value = "username", required = false) String username, HttpSession httpSession) {
        ServerResponse<User> userResponse = userService.findByUsername(username);
        if (userResponse.success()) {
            httpSession.setAttribute("date", userResponse.getData());
        }

        return userResponse;
    }


    //提交问题答案
    @RequestMapping(method = RequestMethod.POST, value = "/forget_check_answer")
    @ResponseBody
    public ServerResponse forget_check_answer(@RequestParam(value = "username", required = false) String username, @RequestParam(value = "question", required = false) String question, @RequestParam(value = "answer") String answer, HttpSession session) {
        ServerResponse serverResponse = userService.forget_check_answer(username, question, answer);
        if (serverResponse.success()) {
//            FlushUuid uuid = new FlushUuid();
//            String taken = uuid.flashToken();
            UuidUtils uuidUtils = new UuidUtils();
            String token = uuidUtils.getUuid();
            Jedis jedis = new Jedis();
            jedis.set("token", token);
            serverResponse.setData(token);
            session.setAttribute("date", serverResponse.getData());
        }
        return serverResponse;
    }

    //忘记密码的重设密码
    @RequestMapping(method = RequestMethod.GET, value = "/forget_reset_password")
    @ResponseBody
    public ServerResponse forget_reset_password(@RequestParam(value = "username", required = false) String username, @RequestParam(value = "passwordNew", required = false) String passwordNew, @RequestParam(value = "forgetToken", required = false) String forgetToken) {
        ServerResponse<User> serverResponse = userService.forget_reset_password(username, passwordNew, forgetToken);
        return serverResponse;
    }


    //登录状态中重置密码
    @RequestMapping(method = RequestMethod.GET, value = "/reset_password")
    @ResponseBody
    public ServerResponse reset_password(@RequestParam(value = "passwordOld", required = false) String passwordOld, @RequestParam(value = "passwordNew", required = false) String passwordNew, HttpSession session) {
        ServerResponse serverResponse = userService.reset_password(passwordOld, passwordNew, session);
        return serverResponse;
    }

    //登录状态更新个人信息
    @RequestMapping(method = RequestMethod.GET, value = "/update_information")
    @ResponseBody
    public ServerResponse update_information(@RequestParam(value = "username", required = false) String username, @RequestParam(value = "email", required = false) String email, @RequestParam(value = "phone", required = false) String phone, @RequestParam(value = "question", required = false) String question, @RequestParam(value = "answer", required = false) String answer, HttpSession session) {
        ServerResponse serverResponse = userService.update_information(username, email, phone, question, answer, session);
        return serverResponse;
    }

    //获取当前登录用户的详细信息，并强制登录
    @RequestMapping(method = RequestMethod.GET, value = "/get_information")
    @ResponseBody
    public ServerResponse get_information(HttpSession session) {
        ServerResponse serverResponse = userService.get_information(session);
        if (serverResponse.success()) {
            session.setAttribute("data", serverResponse.getData());
        }
        return serverResponse;
    }


    //退出登录
    @RequestMapping(method = RequestMethod.GET, value = "/logout.do")
    @ResponseBody
    public ServerResponse logout(HttpSession session) {
        ServerResponse serverResponse = userService.removeSession(session);
        return serverResponse;
    }
}
